﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace SharpMUD.Framework.CorlibExtensions
{
    public static class System_AppDomain
    {
        private readonly static List<System.Reflection.Assembly> ManuallyRequestedAssemblies = new List<System.Reflection.Assembly>();

        public static System.Type GetAssemblyQualifiedType(this AppDomain self, string assemblyQualifiedType)
        {
            return System.Type.GetType(assemblyQualifiedType, true, true);
        //    int firstCommaIndex = assemblyQualifiedType.IndexOf(',');
        //    if(firstCommaIndex<0)
        //        throw new ArgumentException("The argument is not a valid assembly-qualified type name","assemblyQualifiedType");

        //    string typeName = assemblyQualifiedType.Substring(0, firstCommaIndex);
        //    string assemblyName = assemblyQualifiedType.Substring(firstCommaIndex + 1);

        //    Predicate<Assembly> assemblyPredicate =
        //        (a) => a.FullName.Equals(assemblyName, StringComparison.OrdinalIgnoreCase)
        //            || a.GetName().FullName.Equals(assemblyName, StringComparison.OrdinalIgnoreCase);

        //    List<Assembly> assemblies =
        //        (
        //        from b in ManuallyRequestedAssemblies
        //        where assemblyPredicate(b)
        //        select b
        //        ).Union(
        //        from c in self.GetAssemblies()
        //        where assemblyPredicate(c)
        //        select c
        //        ).ToList();
        //    if(assemblies.Count<=0)
        //        assemblies.Add(Assembly.Load(assemblyName));

        //    if(assemblies.Count<=0)
        //        throw new ArgumentException("The given assembly could not be found","assemblyQualifiedType");

        //    var type =
        //        (
        //        from d in assemblies
        //        let t = d.GetType(typeName,false,true)
        //        where t != null
        //        select t
        //        ).FirstOrDefault();
        //    if(type!=null)
        //        if(!ManuallyRequestedAssemblies.Contains(type.Assembly))
        //            ManuallyRequestedAssemblies.Add(type.Assembly);

        //    return type;
        }
    }
}
